home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Turbo Pascal V7 / DOCDEMO.ZIP / STOCKS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-11-03  |  6.2 KB  |  244 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision 2.0 Demo                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit Stocks;
  9.  
  10. interface
  11.  
  12. uses TutConst, Drivers, Objects, TutTypes, Dialogs, Count, Validate;
  13.  
  14. type
  15.   PStockDialog = ^TStockDialog;
  16.   TStockDialog = object(TDialog)
  17.     StockNum, Descrip, Quant, UCost, Supplier: PInputLine;
  18.     Counter: PCountView;
  19.     constructor Init;
  20.     procedure CancelStock;
  21.     procedure EnterNewStock;
  22.     procedure HandleEvent(var Event: TEvent); virtual;
  23.     procedure SaveStockData;
  24.     procedure ShowStock(AStockNum: Integer);
  25.     function Valid(Command: Word): Boolean; virtual;
  26.   end;
  27.   PStockNumValidator = ^TStockNumValidator;
  28.   TStockNumValidator = object(TLookupValidator)
  29.     procedure Error; virtual;
  30.     function Lookup(const S: string): Boolean; virtual;
  31.   end;
  32.  
  33. var
  34.   StockColl: PCollection;
  35.   StockInfo: PStockItem;
  36.   TempStockItem: PStockItemObj;
  37.  
  38. procedure LoadStock;
  39. procedure SaveStock;
  40. procedure RegisterStocks;
  41.  
  42. const
  43.   RStockNumValidator: TStreamRec = (
  44.     ObjType: 994;
  45.     VmtLink: Ofs(TypeOf(TStockNumValidator)^);
  46.     Load: @TStockNumValidator.Load;
  47.     Store: @TStockNumValidator.Store
  48.   );
  49.  
  50.  
  51. implementation
  52.  
  53. uses Views, MsgBox;
  54.  
  55. const
  56.   CurrentStock: Integer = 0;
  57.  
  58. constructor TStockDialog.Init;
  59. var
  60.   R: TRect;
  61. begin
  62.   R.Assign(0, 0, 60, 11);
  63.   inherited Init(R, 'Stock Items');
  64.   Options := Options or ofCentered;
  65.   HelpCtx := $E000;
  66.  
  67.   R.Assign(12, 2, 22, 3);
  68.   StockNum := New(PInputLine, Init(R, 8));
  69.   StockNum^.SetValidator(New(PPXPictureValidator, Init('&&&-####', True)));
  70.   Insert(StockNum);
  71.   R.Assign(2, 2, 12, 3);
  72.   Insert(New(PLabel, Init(R, 'Stock #:', StockNum)));
  73.  
  74.   R.Assign(9, 4, 57, 5);
  75.   Descrip := New(PInputLine, Init(R, 80));
  76.   Insert(Descrip);
  77.   R.Assign(2, 4, 9, 5);
  78.   Insert(New(PLabel, Init(R, 'Item:', Descrip)));
  79.  
  80.   R.Assign(14, 6, 21, 7);
  81.   Quant := New(PInputLine, Init(R, 12));
  82.   Quant^.SetValidator(New(PRangeValidator, Init(1, 99999)));
  83.   Insert(Quant);
  84.   R.Assign(2, 6, 14, 7);
  85.   Insert(New(PLabel, Init(R, '# on hand:', Quant)));
  86.  
  87.   R.Assign(43, 6, 50, 7);
  88.   UCost := New(PInputLine, Init(R, 12));
  89.   Insert(UCost);
  90.   R.Assign(31, 6, 43, 7);
  91.   Insert(New(PLabel, Init(R, 'Unit cost:', UCost)));
  92.  
  93.   R.Assign(2, 8, 12, 10);
  94.   Insert(New(PButton, Init(R, '~N~ew', cmStockNew, bfNormal)));
  95.   R.Assign(13, 8, 23, 10);
  96.   Insert(New(PButton, Init(R, '~S~ave', cmStockSave, bfDefault)));
  97.   R.Assign(24, 8, 34, 10);
  98.   Insert(New(PButton, Init(R, 'Re~v~ert', cmStockCancel, bfNormal)));
  99.   R.Assign(35, 8, 45, 10);
  100.   Insert(New(PButton, Init(R, 'Next', cmStockNext, bfNormal)));
  101.   R.Assign(46, 8, 56, 10);
  102.   Insert(New(PButton, Init(R, 'Prev', cmStockPrev, bfNormal)));
  103.  
  104.   R.Assign(5, 10, 20, 11);
  105.   Counter := New(PCountView, Init(R));
  106.   with Counter^ do
  107.   begin
  108.     SetCount(StockColl^.Count);
  109.     SetCurrent(CurrentStock + 1);
  110.   end;
  111.   Insert(Counter);
  112.   DisableCommands([cmStockPrev]);
  113.   SelectNext(False);
  114. end;
  115.  
  116. procedure TStockDialog.CancelStock;
  117. begin
  118.   if CurrentStock = StockColl^.Count then
  119.   begin
  120.     Dispose(TempStockItem, Done);
  121.     ShowStock(CurrentStock - 1)
  122.   end
  123.   else ShowStock(CurrentStock);
  124. end;
  125.  
  126. procedure TStockDialog.EnterNewStock;
  127. begin
  128.   CurrentStock := StockColl^.Count;
  129.   TempStockItem := New(PStockItemObj, Init);
  130.   StockInfo := @(TempStockItem^.TransferRecord);
  131.   SetData(StockInfo^);
  132.   Counter^.SetCurrent(CurrentStock + 1);
  133.   DisableCommands([cmStockNew, cmStockNext, cmStockPrev]);
  134.   EnableCommands([cmStockCancel, cmStockSave]);
  135. end;
  136.  
  137. procedure TStockDialog.HandleEvent(var Event: TEvent);
  138. begin
  139.   inherited HandleEvent(Event);
  140.   if Event.What = evCommand then
  141.     case Event.Command of
  142.       cmStockNext:
  143.         begin
  144.           ShowStock(CurrentStock + 1);
  145.           ClearEvent(Event);
  146.         end;
  147.       cmStockPrev:
  148.         begin
  149.           ShowStock(CurrentStock - 1);
  150.           ClearEvent(Event);
  151.         end;
  152.       cmStockNew:
  153.         begin
  154.           EnterNewStock;
  155.           ClearEvent(Event);
  156.         end;
  157.       cmStockCancel:
  158.         begin
  159.           CancelStock;
  160.           ClearEvent(Event);
  161.         end;
  162.       cmStockSave:
  163.         begin
  164.           SaveStockData;
  165.           ClearEvent(Event);
  166.         end;
  167.     end;
  168. end;
  169.  
  170. procedure TStockDialog.SaveStockData;
  171. begin
  172.   if Valid(cmClose) then
  173.   begin
  174.     if CurrentStock = StockColl^.Count then StockColl^.Insert(TempStockItem);
  175.     GetData(StockInfo^);
  176.     SaveStock;
  177.   end;
  178.   EnableCommands([cmStockPrev, cmStockNew]);
  179. end;
  180.  
  181. procedure TStockDialog.ShowStock(AStockNum: Integer);
  182. begin
  183.   CurrentStock := AStockNum;
  184.   StockInfo := @PStockItemObj(StockColl^.At(CurrentStock))^.TransferRecord;
  185.   SetData(StockInfo^);
  186.   Counter^.SetCurrent(CurrentStock + 1);
  187.   if CurrentStock > 0 then EnableCommands([cmStockPrev])
  188.   else DisableCommands([cmStockPrev]);
  189.   if StockColl^.Count > 0 then EnableCommands([cmStockNext]);
  190.   if CurrentStock >= StockColl^.Count - 1 then DisableCommands([cmStockNext]);
  191.   EnableCommands([cmStockSave, cmStockNew]);
  192. end;
  193.  
  194. function TStockDialog.Valid(Command: Word): Boolean;
  195. begin
  196.   if Command = cmStockCancel then
  197.     Valid := True
  198.   else Valid := inherited Valid(Command);
  199. end;
  200.  
  201. procedure TStockNumValidator.Error;
  202. begin
  203.   MessageBox('Not a valid stock item number', nil, mfOKButton);
  204. end;
  205.  
  206. function TStockNumValidator.Lookup(const S: string): Boolean;
  207. var
  208.   LookOrder: POrderObj;
  209.  
  210.   function IsInList(P: Pointer): Boolean; far;
  211.   begin
  212.     IsInList := PStockItemObj(P)^.TransferRecord.StockNo = S;
  213.   end;
  214.  
  215. begin
  216.   Lookup := (StockColl^.FirstThat(@IsInList) <> nil);
  217. end;
  218.  
  219. procedure LoadStock;
  220. var
  221.   StockFile: TBufStream;
  222. begin
  223.   StockFile.Init('ITEMS.DAT', stOpenRead, 1024);
  224.   StockColl := PCollection(StockFile.Get);
  225.   StockFile.Done;
  226.   StockInfo := @(PStockItemObj(StockColl^.At(0))^.TransferRecord);
  227. end;
  228.  
  229. procedure SaveStock;
  230. var
  231.   StockFile: TBufStream;
  232. begin
  233.   StockFile.Init('ITEMS.DAT', stOpenWrite, 1024);
  234.   StockFile.Put(StockColl);
  235.   StockFile.Done;
  236. end;
  237.  
  238. procedure RegisterStocks;
  239. begin
  240.   RegisterType(RStockNumValidator);
  241. end;
  242.  
  243. end.
  244.